package com.controller;

import com.github.pagehelper.PageInfo;
import com.entity.User;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 控制层，导入service层
 */
@Controller
public class UserControl {
    @Resource
    private UserService userService;


    //设置默认访问页
    @RequestMapping("/")
    public String hello(){
        return "login.html";
    }

    //-----------------------------登录功能
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @GetMapping("/result") //登录结果跳转
    public String result(HttpServletRequest req, HttpServletResponse resp, HttpSession session){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user=userService.selectUserByName(username);
        boolean flag = userService.login(user);

        if(flag){
            session.setAttribute("name",user.getUsername());
            session.setAttribute("userrole",user.getRoles().getRoleName());

            // 根据用户角色跳转
            int userrole = user.getUserrole(); // 获取用户角色ID
            System.out.println(userrole);

            switch (userrole) {
                case 1: // 系统管理员
                    return "redirect:/list"; // 跳转到管理员控制台
                case 2: // 经理
                    return "redirect:/managerlist"; // 跳转到经理页面
                case 3: // 读者
                    return "redirect:/readerlist"; // 跳转到读者首页
                default:
                    return "redirect:/list"; // 默认跳转到列表页面
            }
        }else {
            return "loginerror";
        }
    }

    @GetMapping("/loginerror") //登录错误
    public String loginerror(){
        return "login";
    }

    //-----------------------------注册功能
    @GetMapping("/register")
    public String register(){
        return "register";
    }
    @GetMapping("/registerend") //注册结果跳转
    public String registerend(HttpServletRequest req, HttpServletResponse resp){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setUserrole(3);
        boolean flag = userService.register(user);
        if(flag==true){
            return "registersuccess";
        }else{
            return "registererror";
        }
    }

    //查询全部
    @RequestMapping("/list")
    public String userList(Model model,String uname,@RequestParam(defaultValue = "1") int pageNum){
        PageInfo<User> pageInfo = userService.findAll(uname,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("uname",uname);
            return"index";
        }else{
            return"error";
        }
    }

    //查询全部
    @RequestMapping("/managerlist")
    public String managerList(Model model,String uname,@RequestParam(defaultValue = "1") int pageNum){
        PageInfo<User> pageInfo = userService.findAll(uname,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("uname",uname);
            return "managerindex";
        }else{
            return"error";
        }
    }

    //查询全部
    @RequestMapping("/readerlist")
    public String readerList(Model model,String uname,@RequestParam(defaultValue = "1") int pageNum){
        PageInfo<User> pageInfo = userService.findAll(uname,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("uname",uname);
            return "readerindex";
        }else{
            return"error";
        }
    }


    //新增数据
    @RequestMapping("/add")
    public String save(User user){
        userService.save(user);
        return "redirect:/list";
    }
    @RequestMapping("/useradd")
    public String add(){
        return "add";
    }

    //删除数据
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
        int count =userService.delete(id);
        if(count==1){
            servletResponse.sendRedirect("/list");
        }
        return "error";
    }

    //根据id查找
    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model,@PathVariable int id){
        User users = userService.get(id);
        model.addAttribute("users",users);
        return "update";
    }

    //更新数据
    @PostMapping("/update")
    public String updateUser(Model model,User user,HttpServletRequest request){
        String id = request.getParameter("id");
        User userById = userService.get(Integer.parseInt(id));
        userService.update(user);
//        System.out.println(user);
        return "redirect:/list";
    }

    //根据id查找
    @GetMapping("/readerusercheckPage/{id}")
    public String readerusercheckPage(Model model,@PathVariable int id){
        User users = userService.get(id);
        model.addAttribute("users",users);
        return "readerusercheck";
    }

    //根据id查找
    @GetMapping("/managerusercheckPage/{id}")
    public String managerusercheckPage(Model model,@PathVariable int id){
        User users = userService.get(id);
        model.addAttribute("users",users);
        return "managerusercheck";
    }


}

